<template>
  <div class="start-box" v-if="props.nodeConfig.nodeType == 'startNode'">
    <span class="start-label"> {{ props.nodeConfig.formData.nodeName }} </span>
    <div class="add-node-btn-box"></div>
  </div>
  <div class="node-wrap" v-if="props.nodeConfig.nodeType === 'approvalNode' || props.nodeConfig.nodeType === 'ccNode'">
    <div
      class="node-wrap-box"
      :class="
        store.state.tree.isTried && props.nodeConfig.error
          ? 'active error'
          : store.state.tree.deployFormData.nodeId === props.nodeConfig.nodeId
          ? 'active'
          : ''
      "
    >
      <div>
        <div
          class="title"
          :class="
            props.nodeConfig.nodeType == 'approvalNode' && props.nodeConfig.runtimeFlag !== null
              ? ' approval'
              : props.nodeConfig.nodeType === 'ccNode' && props.nodeConfig.runtimeFlag !== null
              ? ' cc'
              : props.nodeConfig.runtimeFlag === null
              ? ' invalid'
              : ''
          "
        >
          <span class="zcb zcbjd_sp" v-show="props.nodeConfig.nodeType == 'approvalNode'"></span>
          <span class="zcb zcbjd_cs" v-show="props.nodeConfig.nodeType == 'ccNode'"></span>
          <span class="editable-title">{{ props.nodeConfig.formData.nodeName }} </span>
        </div>
        <div class="content">
          <div class="text" v-if="props.nodeConfig.nodeType == 'approvalNode'">
            <span class="placeholder" v-if="!props.nodeConfig.formData.approvalName?.length">{{ t('keys.pleaseSelect') }}</span>
            <span class="placeholder" v-else>
              <template v-for="(item, index) in props.nodeConfig.formData.approvalName" :key="index">
                {{ props.nodeConfig.formData.approvalName?.length > 1 ? item.name + '、' : item.name }}
              </template>
            </span>
          </div>
          <div class="text" v-if="props.nodeConfig.nodeType == 'ccNode'">
            <span class="placeholder" v-if="!props.nodeConfig.formData.ccName?.length">{{ t('keys.pleaseSelect') }}</span>
            <span class="placeholder" v-else>
              <template v-for="(item, index) in props.nodeConfig.formData.ccName" :key="index">
                {{ props.nodeConfig.formData.ccName?.length > 1 ? item.name + '、' : item.name }}
              </template>
            </span>
          </div>
          <div class="type">{{ props.nodeConfig.formData.valueContent }}</div>
          <div class="icon-status" v-if="props.nodeConfig.runtimeFlag && props.nodeConfig.runtimeFlag !== null">
            <!-- 审批节点 -->
            <template v-if="props.nodeConfig.nodeType == 'approvalNode'">
              <img v-if="props.nodeConfig.runtimeFlag === 'PASS'" src="@assets/images/icon-pass.png" alt="" />
              <img v-if="props.nodeConfig.runtimeFlag === 'REFUSE'" src="@assets/images/icon-fail.png" alt="" />
            </template>
            <!-- 抄送 -->
            <template v-if="props.nodeConfig.nodeType == 'ccNode'">
              <!-- <img v-if="props.nodeConfig.runtimeFlag === 'CC'" src="@assets/images/icon-pass.png" alt="" /> -->
              <!-- <img v-if="props.nodeConfig.runtimeFlag" src="@assets/images/icon-fail.png" alt="" /> -->
            </template>
            <span class="error">{{
              props.nodeConfig.runtimeFlag === 'ERROR' ?  t('keys.Process_exception')  : props.nodeConfig.runtimeFlag === 'STOP' ?  t('keys.Process_Suspend') : ''
            }}</span>
          </div>
        </div>
      </div>
    </div>
    <div class="add-node-btn-box "></div>
  </div>
  <div class="branch-wrap" v-if="props.nodeConfig.nodeType == 'routingNode'">
    <div class="branch-box-wrap">
      <div class="branch-box ">
        <div class="col-box" v-for="(item, index) in props.nodeConfig.condition" :key="index">
          <div class="condition-node">
            <div class="condition-node-box ">
              <el-popover placement="top" :width="500" trigger="hover" v-if="item.formData.triggerType === 'no'">
                <template #default>
                  <popover-item :conditionExpression="item.formData.conditionExpression"></popover-item>
                  <!-- <span>{{ item.formData.conditionExpression }}</span> -->
                </template>
                <template #reference>
                  <div class="auto-judge" :class="item.runtimeFlag === null ? ' invalid' : ''">
                    <div class="title-wrapper">
                      <span class="zcb zcbjd_pd"></span>
                      <span class="editable-title">{{ item.formData.nodeName }}</span>
                      <span class="priority-title" v-if="item.formData.triggerType === 'default'">{{ t('keys.default') }}</span>
                    </div>
                  </div>
                </template>
              </el-popover>

              <div class="auto-judge" :class="item.runtimeFlag === null ? ' invalid' : ''" v-else>
                <div class="title-wrapper">
                  <span class="zcb zcbjd_pd"></span>
                  <span class="editable-title">{{ item.formData.nodeName }}</span>
                  <span class="priority-title" v-if="item.formData.triggerType === 'default'">{{ t('keys.default') }}</span>
                </div>
              </div>
              <!-- <div class="add-node-btn-box"></div> -->
              <!-- <add-node v-model:childrenP="item.children"></add-node> -->
            </div>
          </div>
          <flow-chart v-if="item.children" v-model:nodeConfig="item.children"></flow-chart>
          <div class="top-left-cover-line" v-if="index == 0"></div>
          <div class="bottom-left-cover-line" v-if="index == 0"></div>
          <div class="top-right-cover-line" v-if="index == props.nodeConfig.condition.length - 1"></div>
          <div class="bottom-right-cover-line " v-if="index == props.nodeConfig.condition.length - 1"></div>
        </div>
      </div>
      <div class="add-node-btn-box"></div>
    </div>
  </div>
  <flow-chart v-if="props.nodeConfig.children" v-model:nodeConfig="props.nodeConfig.children"></flow-chart>
</template>
<script setup>
import { useStore } from 'vuex'
import popoverItem from './popoverItem.vue'
import { useI18n } from 'vue-i18n'
const { t } = useI18n() // 中英文
const props = defineProps({
  nodeConfig: {
    type: Object,
    default: () => {}
  }
})
const store = useStore()
</script>
<style lang="scss" scoped>
.add-node-btn-box {
  width: 240px;
  display: -webkit-inline-box;
  display: -ms-inline-flexbox;
  display: inline-flex;
  -ms-flex-negative: 0;
  flex-shrink: 0;
  -webkit-box-flex: 1;
  -ms-flex-positive: 1;
  position: relative;
  z-index: 9;
  height: 80px;
  &:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    width: 2px;
    height: 100%;
    background-color: #cacaca;
  }
}

.start-box {
  .start-label {
    width: 120px;
    height: 40px;
    background: #ffffff;
    box-shadow: 0px 4px 8px 0px rgba(0, 0, 0, 0.15);
    border-radius: 4px;
    font-size: 14px;
    font-weight: 400;
    color: #333333;
    line-height: 22px;
    margin: 0 auto;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    cursor: pointer;
  }
}
.error_tip {
  position: absolute;
  top: 0px;
  right: 0px;
  transform: translate(150%, 0px);
  font-size: 24px;
}
.promoter_person .el-dialog__body {
  padding: 10px 20px 14px 20px;
}
.selected_list {
  margin-bottom: 20px;
  line-height: 30px;
  span {
    margin-right: 10px;
    padding: 3px 6px 3px 9px;
    line-height: 12px;
    white-space: nowrap;
    border-radius: 2px;
    border: 1px solid rgba(220, 220, 220, 1);
  }
  img {
    margin-left: 5px;
    width: 7px;
    height: 7px;
    cursor: pointer;
  }
}

.editable-title {
  line-height: 15px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  margin-left: 8px;
  border-bottom: 1px dashed transparent;
}

.ant-btn {
  position: relative;
}

.node-wrap-box {
  display: -webkit-inline-box;
  display: -ms-inline-flexbox;
  display: inline-flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
  position: relative;
  width: 240px;
  min-height: 72px;
  -ms-flex-negative: 0;
  flex-shrink: 0;
  background: #fff;
  border-radius: 4px;
  margin-top: 6px;
  box-shadow: 0px 4px 8px 0px rgba(0, 0, 0, 0.15);
  cursor: pointer;
  &::after {
    content: '';
    pointer-events: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 2;
    border-radius: 4px;
    border: 1px solid transparent;
    transition: all 0.1s cubic-bezier(0.645, 0.045, 0.355, 1);
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.1);
  }
  .title {
    position: relative;
    display: flex;
    align-items: center;
    padding-left: 8px;
    padding-right: 30px;
    height: 36px;
    line-height: 36px;
    font-size: 14px;
    color: #fff;
    text-align: left;
    background: #576a95;
    border-radius: 4px 4px 0 0;
    .iconfont {
      font-size: 12px;
      margin-right: 5px;
    }
  }
  .placeholder {
    color: #333333;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    display: block;
    width: 100%;
    line-height: 22px;
  }
  .close {
    display: none;
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    width: 20px;
    height: 20px;
    font-size: 20px;
    color: #fff;
    border-radius: 50%;
    text-align: center;
    line-height: 20px;
  }
  &.active,
  &:active,
  &:hover {
    .close {
      display: block;
    }
  }
  &.active::after,
  &:active::after,
  &:hover::after {
    border: 1px solid #3296fa;
    box-shadow: 0 0 6px 0 rgba(50, 150, 250, 0.3);
  }
  &.error::after {
    border: 1px solid #f25643;
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.1);
  }
  &:before {
    content: '';
    position: absolute;
    top: -12px;
    left: 50%;
    -webkit-transform: translateX(-50%);
    transform: translateX(-50%);
    width: 0;
    height: 4px;
    border-style: solid;
    border-width: 8px 6px 4px;
    border-color: #cacaca transparent transparent;
    background: #fafafa;
  }
  .approval {
    background: rgba(255, 103, 0, 0.6);
  }
  .cc {
    background: rgba(72, 208, 0, 0.6);
  }
  .invalid {
    background: #cccccc;
  }
  &:hover,
  &.active {
    .approval {
      background: rgba(255, 103, 0, 1);
    }
    .cc {
      background: rgba(72, 208, 0, 1);
    }
    .invalid {
      background: #cccccc;
    }
  }
  .content {
    position: relative;
    font-size: 14px;
    padding: 12px;
    .text {
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 3;
      -webkit-box-orient: vertical;
    }
    .type {
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 3;
      -webkit-box-orient: vertical;
      color: #333333;
      margin-top: 8px;
      line-height: 22px;
    }
    .arrow {
      position: absolute;
      right: 10px;
      top: 50%;
      transform: translateY(-50%);
      width: 20px;
      height: 14px;
      font-size: 14px;
      color: #979797;
    }
    .icon-status {
      width: 67px;
      height: 67px;
      position: absolute;
      top: 5px;
      right: 12px;
      img {
        width: 100%;
      }
      .error {
        font-size: 14px;
        font-weight: 400;
        color: #ff4a4a;
        line-height: 22px;
        position: absolute;
        bottom: 7px;
        right: 0;
      }
    }
  }
  &.start-node {
    .content {
      .text {
        display: block;
        white-space: nowrap;
      }
    }
    &::before {
      content: none;
    }
  }
}

.top-left-cover-line {
  left: -1px;
}

.top-right-cover-line {
  right: -1px;
}

.bottom-left-cover-line {
  left: -1px;
}
.bottom-right-cover-line {
  right: -1px;
}
.top-left-cover-line,
.top-right-cover-line {
  position: absolute;
  height: 8px;
  width: 50%;
  background-color: #fafafa;
  top: -4px;
}

.bottom-left-cover-line,
.bottom-right-cover-line {
  position: absolute;
  height: 8px;
  width: 50%;
  background-color: #fafafa;
  bottom: -4px;
}

.box-scale {
  transform: scale(1);
  display: inline-block;
  position: relative;
  width: 100%;
  padding: 54.5px 0;
  -webkit-box-align: start;
  -ms-flex-align: start;
  align-items: flex-start;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  min-width: -webkit-min-content;
  min-width: -moz-min-content;
  min-width: min-content;
  background-color: #fafafa;
  -webkit-transform-origin: 0 0 0;
  transform-origin: 0 0 0;
}

.node-wrap {
  flex-direction: column;
  -webkit-box-pack: start;
  -ms-flex-pack: start;
  justify-content: flex-start;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  -webkit-box-flex: 1;
  -ms-flex-positive: 1;
  // padding: 0 50px;
  position: relative;
}

.branch-wrap,
.node-wrap {
  display: inline-flex;
}

.branch-box-wrap {
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  min-height: 270px;
  width: 100%;
  -ms-flex-negative: 0;
  flex-shrink: 0;
}

.branch-box {
  display: flex;
  overflow: visible;
  min-height: 180px;
  height: auto;
  border-bottom: 2px solid #ccc;
  border-top: 2px solid #ccc;
  position: relative;
  .col-box {
    background: #fafafa;
    margin-right: -1px;
    &:before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      z-index: 0;
      margin: auto;
      width: 2px;
      height: 100%;
      background-color: #cacaca;
    }
  }
}

.add-branch {
  border: none;
  outline: none;
  user-select: none;
  justify-content: center;
  font-size: 12px;
  padding: 0 10px;
  height: 30px;
  line-height: 30px;
  border-radius: 15px;
  color: #3296fa;
  background: #fff;
  border: 1px solid #3296fa;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
  position: absolute;
  top: -16px;
  left: 50%;
  transform: translateX(-50%);
  transform-origin: center center;
  cursor: pointer;
  z-index: 1;
  display: inline-flex;
  align-items: center;
  -webkit-transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  &:hover {
    transform: translateX(-50%) scale(1.1);
    box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.1);
  }
  &:active {
    transform: translateX(-50%);
    box-shadow: none;
  }
}

.col-box {
  display: inline-flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  flex-direction: column;
  -webkit-box-align: center;
  align-items: center;
  position: relative;
}

.condition-node {
  min-height: 220px;
}

.condition-node,
.condition-node-box {
  display: inline-flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  flex-direction: column;
  -webkit-box-flex: 1;
}

.condition-node-box {
  padding-top: 30px;
  padding-right: 50px;
  padding-left: 50px;
  -webkit-box-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  align-items: center;
  flex-grow: 1;
  position: relative;
  &:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    width: 2px;
    height: 100%;
    background-color: #cacaca;
  }
}

.auto-judge {
  width: 240px;
  height: 36px;
  border-radius: 4px;
  position: relative;
  display: flex;
  align-items: center;
  cursor: pointer;
  background-color: #cacaca;
  box-shadow: 0px 4px 8px 0px rgba(0, 0, 0, 0.15);
  &::before {
    pointer-events: none;
    content: '';
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    border-radius: 4px;
    background: rgba(0, 119, 255, 0.6);
    border: 1px solid transparent;
    transition: all 0.1s cubic-bezier(0.645, 0.045, 0.355, 1);
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.1);
  }
  &.active::before,
  &:active::before,
  &:hover::before {
    background: rgba(0, 119, 255, 1);
    box-shadow: 0 0 6px 0 rgba(50, 150, 250, 0.3);
  }
  &.active .close,
  &:active .close,
  &:hover .close {
    display: block;
  }

  &:hover .sort-left,
  &:hover .sort-right {
    display: flex;
    align-items: center;
  }
  &::after {
    pointer-events: none;
    content: '';
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    border-radius: 4px;
    border: 1px solid transparent;
  }
  &.error:after {
    border: 1px solid #f25643;
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.1);
  }
  &.invalid::before {
    background: #cccccc;
  }
  .title-wrapper {
    position: relative;
    font-size: 14px;
    color: #ffffff;
    text-align: left;
    line-height: 22px;
    font-weight: 500;
    margin: 8px auto 8px 8px;
    display: flex;
    align-items: center;
    width: 100%;

    .editable-title {
      display: inline-block;
      max-width: 120px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      margin-left: 8px;
    }

    .priority-title {
      margin-right: 10px;
      border-radius: 2px;
      border: 1px solid #ffffff;
      font-size: 12px;
      font-weight: 400;
      color: #ffffff;
      line-height: 18px;
      padding: 0 8px;
      margin-left: 4px;
    }
  }
  .placeholder {
    color: #bfbfbf;
  }

  .close {
    display: none;
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    width: 20px;
    height: 20px;
    font-size: 20px;
    color: #fff;
    border-radius: 50%;
    text-align: center;
    line-height: 20px;
  }

  .content {
    font-size: 14px;
    color: #191f25;
    text-align: left;
    margin-top: 6px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
  }

  .sort-left,
  .sort-right {
    position: absolute;
    top: 0;
    bottom: 0;
    display: none;
    z-index: 1;
  }

  .sort-left {
    left: 0;
    border-right: 1px solid #f6f6f6;
  }

  .sort-right {
    right: 0;
    border-left: 1px solid #f6f6f6;
  }

  .sort-left:hover,
  .sort-right:hover {
    background: #efefef;
  }
}

.end-node {
  border-radius: 50%;
  font-size: 14px;
  color: rgba(25, 31, 37, 0.4);
  text-align: left;
  .end-node-circle {
    width: 10px;
    height: 10px;
    margin: auto;
    border-radius: 50%;
    background: #dbdcdc;
  }
  .end-node-text {
    margin-top: 5px;
    text-align: center;
  }
}

.approval-setting {
  border-radius: 2px;
  margin: 20px 0;
  position: relative;
  background: #fff;
}

.ant-btn {
  position: relative;
}
</style>
